package com.jike.noobmoney.mvp.view.activity;


import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.adapter.NewListAdapter;
import com.jike.noobmoney.adapter.TaskListAdapter;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.NewEntity;
import com.jike.noobmoney.entity.TaskListEntity;
import com.jike.noobmoney.mvp.presenter.TaskPresenter;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.SPUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by kekex on 2019/4/13
 * 公告列表
 */

public class NewListActivity extends BaseActivity implements IView {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty)
    ImageView empty;


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    private TaskPresenter taskPresenter;
    private NewListAdapter adapter;
    private boolean isRefresh = true;
    private int page = 1;
    private List<NewEntity.MessageBean> mData = new ArrayList<>();
    private String userid;
    private View headerView;


    @Override
    protected void onResume() {
        super.onResume();
        loadData(true);
    }

    @Override
    protected int layoutResID() {
        return R.layout.activity_new_list;
    }
    @Override
    public boolean ismain() {
        return true;
    }
    @Override
    public Boolean isfits() {
        return true;
    }
    @Override
    protected void initData() {
        //官方推荐 改 热门任务
        tvTitle.setText("平台公告");
        tvRight.setText("");

        taskPresenter = new TaskPresenter(this);

        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);

        //adapter
        adapter = new NewListAdapter(context);

        //headerView = LayoutInflater.from(context).inflate(R.layout.layout_gftj, null, false);
        //manager
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);
        //adapter.setHeaderView(headerView);

        //refreshLayout.autoRefresh();  //自动刷新
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            loadData(true);
        });

        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            loadData(false);
        });

        loadData(true);

        //item点击
        adapter.setOnItemClickListener((adapterView, view, position, l) -> {
            NewEntity.MessageBean item = (NewEntity.MessageBean) adapter.getItem(position);
            Intent intent = new Intent(context, NewDetailActivity.class);
            intent.putExtra("model",item);
            startActivity(intent);
        });
    }

    /**
     *
     */
    private void loadData(boolean isRefresh) {
        this.isRefresh = isRefresh;
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }

        if (taskPresenter != null) {
            showProgress();
            taskPresenter.getNewlist(userid, "" + page, "10", ConstantValue.RequestKey.newlist);
        }
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        disLoading();
        if (ConstantValue.RequestKey.newlist.equals(actionType)) {
            List<NewEntity.MessageBean> task = (List<NewEntity.MessageBean>) data;
            if (task != null && task.size() > 0) {
                if (isRefresh) {
                    mData.clear();
                    mData.addAll(task);
                } else {
                    mData.addAll(task);
                }
            }

            if (empty != null) {
                if (mData.size() > 0) {
                    empty.setVisibility(View.GONE);
                } else {
                    empty.setVisibility(View.VISIBLE);
                }
            }
            adapter.refresh(mData);
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        disLoading();
    }

    private void disLoading() {
        dismissProgress();
        if (refreshLayout != null) {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
        }
    }


    @OnClick({R.id.iv_back, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_right:
                //startActivity(new Intent(context, GuizeActivity.class));
                break;
        }
    }

}
